package com.nbot.server.controller;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nbot.server.domain.ViewChecklogDO;
import com.nbot.server.service.ViewChecklogService;
import com.nbot.server.util.PageUtils;
import com.nbot.server.util.Query;
import com.nbot.server.util.R;
import com.nbot.server.annotation.Log;

/**
 * VIEW
 *
 * @author nbot
 * @email nbot@163.com
 * @date 2018-07-22 11:53:54
 */

@Controller
@RequestMapping("/server/viewChecklog")
public class ViewChecklogController {
    @Autowired
    private ViewChecklogService viewChecklogService;

    @GetMapping()
    @RequiresPermissions("server:viewChecklog:viewChecklog")
    String ViewChecklog() {
        return "server/viewChecklog/viewChecklog" ;
    }

    @ResponseBody
    @Log("查看VIEW")
    @GetMapping("/list")
    @RequiresPermissions("server:viewChecklog:viewChecklog")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Map<String,Object> mapTemp = new HashMap<String,Object>();
        ViewChklogCountController.CovertMapTime(query, mapTemp);
        int total = 0;

        List<ViewChecklogDO> viewChklogCountList = new LinkedList<ViewChecklogDO>();
        viewChklogCountList = viewChecklogService.listLev(mapTemp);
        total = viewChecklogService.countLev(mapTemp);

        /*
        String schType = "0";
        if(mapTemp.get("schType")!=null)
            schType= mapTemp.get("schType").toString();

        List<ViewChecklogDO> viewChklogCountList = new LinkedList<ViewChecklogDO>();
        List<ViewChecklogDO> viewChklogCountListTemp;

        String schDesc;

        if(schType.equals("1"))
        {
            viewChklogCountList = viewChecklogService.listLev(mapTemp);
            total = viewChecklogService.countLev(mapTemp);
        }else
        {
            if(mapTemp.get("dispdesc")!=null && !mapTemp.get("dispdesc").equals(""))
            {
                schDesc= mapTemp.get("dispdesc").toString();
                viewChklogCountListTemp = viewChecklogService.listDesc(mapTemp);
                if(viewChklogCountListTemp.size()>0)
                {
                    viewChklogCountListTemp.get(0).setCountNum(viewChklogCountListTemp.size());
                    viewChklogCountList.add(viewChklogCountListTemp.get(0));
                    total = 1;
                }else
                {
                    PageUtils pageUtils = null;
                    return pageUtils;
                }
            }
            else
            {
                PageUtils pageUtils = null;
                return pageUtils;
            }
        }
        */

        PageUtils pageUtils = new PageUtils(viewChklogCountList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("server:viewChecklog:add")
    String add() {
        return "server/viewChecklog/add" ;
    }

    @GetMapping("/edit/{autoid}")
    @RequiresPermissions("server:viewChecklog:edit")
    String edit(@PathVariable("autoid") Long autoid, Model model) {
            ViewChecklogDO viewChecklog = viewChecklogService.get(autoid);
        model.addAttribute("viewChecklog" , viewChecklog);
        return "server/viewChecklog/edit" ;
    }

    /**
     * 保存
     */
    @Log("保存VIEW")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("server:viewChecklog:add")
    public R save(ViewChecklogDO viewChecklog) {
        if (viewChecklogService.save(viewChecklog) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("修改VIEW")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("server:viewChecklog:edit")
    public R update(ViewChecklogDO viewChecklog) {
            viewChecklogService.update(viewChecklog);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除VIEW")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("server:viewChecklog:remove")
    public R remove( Long autoid) {
        if (viewChecklogService.remove(autoid) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @Log("批量删除VIEW")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("server:viewChecklog:batchRemove")
    public R remove(@RequestParam("ids[]") Long[]autoids) {
            viewChecklogService.batchRemove(autoids);
        return R.ok();
    }

    @GetMapping( "/almLev")
    @ResponseBody
    public List<ViewChecklogDO> listAlmLev(){
        return viewChecklogService.listAlmLev();
    }

}
